package org.cyb.demo.controller;

import org.apache.commons.io.FileUtils;
import org.cyb.demo.domain.Chapter;
import org.cyb.demo.domain.Course;
import org.cyb.demo.domain.Section;
import org.cyb.demo.domain.Video;
import org.cyb.demo.service.ChapterService;
import org.cyb.demo.service.CourseService;
import org.cyb.demo.service.SectionService;
import org.cyb.demo.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.File;

@Controller
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private SectionService sectionService;

    //下载视频
    @RequestMapping("/videoDownload")
    public ResponseEntity<byte[]> videoDownload(
            Video video,
            Course course,
            HttpSession httpSession
    ) throws Exception{
        //根据name查询video
        Video target = videoService.findOneByName(video);
        String fileName = target.getVideoName();
        String path = httpSession.getServletContext().getRealPath("/upload/video/");
        path = path + File.separator + course.getCourseName();
        //获取要下载文件的File对象
        File file = new File(path + File.separator + fileName);
        // 创建springframework的HttpHeaders对象,使得浏览器响应下载
        HttpHeaders headers = new HttpHeaders();
        // 下载显示的文件名，解决中文名称乱码问题
        String downloadFielName = new String(fileName.getBytes("UTF-8"),"iso-8859-1");
        // 通知浏览器以attachment（下载方式）打开视频
        headers.setContentDispositionFormData("attachment", downloadFielName);
        // 设置响应方式    application/octet-stream ： 二进制流数据（最常见的文件下载）。
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        // 把文件以二进制形式写回    201 HttpStatus.CREATED
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
                headers, HttpStatus.CREATED);
    }

    //点击节名称，跳转到播放视频页面
    @RequestMapping("playVideo")
    public ModelAndView playVideo(
            Course course,
            Chapter chapter,
            Section section,
            ModelAndView modelAndView
    ){
        Course targetCourse = courseService.getOneById(course);
        Chapter targetChapter = chapterService.findOneById(chapter);
        Section targetSection = sectionService.findOneById(section);
        modelAndView.addObject("course",targetCourse);
        modelAndView.addObject("chapter",targetChapter);
        modelAndView.addObject("section",targetSection);
        modelAndView.setViewName("student/video");
        return modelAndView;
    }
}
